//
// Created by xili on 2024/9/21 15:43.
// GO BIG OR GO HOME.
//
#include "leetcode.h"

class Solution1 {
public:
    vector<int> advantageCount(vector<int> &nums1, vector<int> &nums2) {
        vector<int> ans;
        int n = nums1.size();
        ranges::sort(nums1);
        for (int i = 0; i < n; i++) {
            auto it = upper_bound(nums1.begin(), nums1.end(), nums2[i]);
            if (it == nums1.end()) {
                it = min_element(nums1.begin(), nums1.end());
            }
            ans.push_back(*it);
            nums1.erase(it);
        }
        return ans;
    }
};

class Solution2 {
public:
    vector<int> advantageCount(vector<int> &nums1, vector<int> &nums2) {
        ranges::sort(nums1);
        int n = nums1.size();
        vector<int> ids(n);
        iota(ids.begin(), ids.end(), 0);
        ranges::sort(ids, [&](int i, int j) { return nums2[i] < nums2[j]; });
        vector<int> ans(n);
        int left = 0, right = n - 1;
        for (int x: nums1) {
            ans[x > nums2[ids[left]] ? ids[left++] : ids[right--]] = x;
        }
        return ans;
    }
};